package com.ruoyi.quartz.race.tool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.quartz.race.model.EuEntity;
import com.ruoyi.quartz.race.model.RaceEntity;
import com.ruoyi.quartz.race.model.RaceStatusEntity;
import com.ruoyi.quartz.util.JedisUtil;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

/**
 * 获取所有赛事及当前赛季
 */
public class TrendUtil implements PageProcessor {
	private Site site = Site.me().setDomain("500.com").setSleepTime(10000)
			.setUserAgent(
					"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31")
			.addHeader("referer", "https://odds.500.com").addHeader("sec-fetch-dest", "empty")
			.addHeader("sec-fetch-mode", "cors").addHeader("sec-fetch-site", "same-origin")
			.addHeader("x-requested-with", "XMLHttpRequest");
	private RaceEntity raceEntity;

	public TrendUtil(RaceEntity ce) {
		this.raceEntity = ce;
	}

	@Override
	public void process(Page page) {
		String pagetext = page.getRawText();
		String apptext = TrendUtilAppend.getHtml(this.raceEntity.getId());
		pagetext = pagetext.replace("id=\"datatb\">", "id=\"datatb\">" + apptext);

		Selectable pds = new Html(pagetext).$("#datatb tr");
		List<Selectable> trs = pds.nodes();

		Map<String, EuEntity> euList = new HashMap<String, EuEntity>();

		for (int i = 0; i < trs.size(); i++) {
			Selectable html = trs.get(i);
			String id = html.$("tr", "id").get();
			String dataTime = html.$("tr", "data-time").get();
			if (id != null && !id.equals("") && !id.equals("step_line")) {
				List<Selectable> tds = html.$("td").nodes();
				Selectable tds0 = tds.get(0);
				Selectable tds1 = tds.get(1);
				Selectable tds2 = tds.get(2);
				Selectable tds19 = tds.get(19);
				String title = tds1.$("td", "title").get();
				// System.out.println(id + "--" + tds1.$("td", "title").get());
				List<Selectable> tds26 = tds2.$(".pl_table_data td").nodes();

				String bw = tds26.get(0).$("td", "text").get();
				String bd = tds26.get(1).$("td", "text").get();
				String bl = tds26.get(2).$("td", "text").get();
				//
				String ew = tds26.get(3).$("td", "text").get();
				String ed = tds26.get(4).$("td", "text").get();
				String el = tds26.get(5).$("td", "text").get();

				List<Selectable> tdskl = tds19.$(".pl_table_data td").nodes();

				String bwkl = tdskl.get(0).$("td", "text").get();
				String bdkl = tdskl.get(1).$("td", "text").get();
				String blkl = tdskl.get(2).$("td", "text").get();
				//
				String ewkl = tdskl.get(3).$("td", "text").get();
				String edkl = tdskl.get(4).$("td", "text").get();
				String elkl = tdskl.get(5).$("td", "text").get();

				EuEntity rjc = new EuEntity();
				rjc.setId(this.raceEntity.getId());
				rjc.setComid(id);
				rjc.setName(title);
				rjc.setInitwin(Float.parseFloat(bw));
				rjc.setInitdraw(Float.parseFloat(bd));
				rjc.setInitlose(Float.parseFloat(bl));
				rjc.setNewwin(Float.parseFloat(ew));
				rjc.setNewdraw(Float.parseFloat(ed));
				rjc.setNewlose(Float.parseFloat(el));

				rjc.setBwkl(Float.parseFloat(bwkl));
				rjc.setBdkl(Float.parseFloat(bdkl));
				rjc.setBlkl(Float.parseFloat(blkl));

				rjc.setEwkl(Float.parseFloat(ewkl));
				rjc.setEdkl(Float.parseFloat(edkl));
				rjc.setElkl(Float.parseFloat(elkl));

				euList.put(id, rjc);
			}
		}

		//////////////////// 计算部分

		// 凯利大于1.03的数量
		int wnumkl = 0;
		int dnumkl = 0;
		int lnumkl = 0;
		for (Map.Entry<String, EuEntity> entry : euList.entrySet()) {
			EuEntity rjc = entry.getValue();
			//
			if (rjc.getBwkl() > 1.03) {
				wnumkl = wnumkl + 1;
			}

			if (rjc.getBdkl() > 1.03) {
				dnumkl = dnumkl + 1;
			}

			if (rjc.getBlkl() > 1.03) {
				lnumkl = lnumkl + 1;
			}
		}

		// 赔率降低的数量
		int allnum = euList.size();
		int wnumlt = 0;
		int dnumlt = 0;
		int lnumlt = 0;

		for (Map.Entry<String, EuEntity> entry : euList.entrySet()) {
			EuEntity rjc = entry.getValue();
			//
			if (rjc.getNewwin() <= rjc.getInitwin()) {
				wnumlt = wnumlt + 1;
			}

			if (rjc.getNewdraw() <= rjc.getInitdraw()) {
				dnumlt = dnumlt + 1;
			}

			if (rjc.getNewlose() <= rjc.getInitlose()) {
				lnumlt = lnumlt + 1;
			}
		}

		Calendar myTime = Calendar.getInstance();
		Date beforeM = myTime.getTime();
		String cdate = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(beforeM);

		RaceStatusEntity rse = new RaceStatusEntity();
		rse.setId(this.raceEntity.getId());
		rse.setCdate(cdate);
		rse.setEuList(euList);

		rse.setAllnum(allnum);
		rse.setWnumlt(wnumlt);
		rse.setDnumlt(dnumlt);
		rse.setLnumlt(lnumlt);

		rse.setKlwnum(wnumkl);
		rse.setKldnum(dnumkl);
		rse.setKllnum(lnumkl);

		EuEntity euwlkl = euList.get("293");

		rse.setBwklwl(euwlkl.getBwkl());
		rse.setBdklwl(euwlkl.getBdkl());
		rse.setBlklwl(euwlkl.getBlkl());

		rse.setEwklwl(euwlkl.getEwkl());
		rse.setEdklwl(euwlkl.getEdkl());
		rse.setElklwl(euwlkl.getElkl());

		if (allnum > 50) {
			// download
			if (allnum - wnumlt <= 6) {
				rse.setWst(1);
			}
			if (allnum - dnumlt <= 6) {
				rse.setDst(1);
			}
			if (allnum - lnumlt <= 6) {
				rse.setLst(1);
			}
			/////////////////////////////// 3p
			String wlid = "293";
			String lbid = "2";
			String interid = "4";
			EuEntity euwl = euList.get(wlid);
			EuEntity eulb = euList.get(lbid);
			EuEntity euinter = euList.get(interid);

			if (euwl != null && eulb != null && euinter != null) {
				if (eulb.getInitwin() <= euwl.getInitwin() && euwl.getInitwin() <= euinter.getInitwin()) {
					rse.setP3win(1);
				}
				if (eulb.getInitdraw() <= euwl.getInitdraw() && euwl.getInitdraw() <= euinter.getInitdraw()) {
					rse.setP3draw(1);
				}
				if (eulb.getInitlose() <= euwl.getInitlose() && euwl.getInitlose() <= euinter.getInitlose()) {
					rse.setP3lose(1);
				}
			}
			/////////////////////////////// 3p
			rse.setWllb("");
			if (rse.getWst() == 1 && rse.getP3win() == 1) {
				rse.setWllb(rse.getWllb() + " WIN ");
			}
			if (rse.getDst() == 1 && rse.getP3draw() == 1) {
				rse.setWllb(rse.getWllb() + " DRAW ");
			}
			if (rse.getLst() == 1 && rse.getP3lose() == 1) {
				rse.setWllb(rse.getWllb() + " LOSE ");
			}
		}

		rse.setBetflag(getComString(euList));

		String key = "trend:" + rse.getId();
		JedisUtil.set(key, JSON.toJSONString(rse));
		////
		RealTimeUtil.getHtml(rse.getId());
	}

	private String getComString(Map<String, EuEntity> euList) {
		Map<String, String> spcList = new HashMap();
		spcList.put("BetFlat", "772");
		spcList.put("InterWin", "4");
		spcList.put("Pinnacle平博", "1055");
		spcList.put("Stanleybet", "216");
		String result = "";
		Iterator<Map.Entry<String, String>> iterator = spcList.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			String key = entry.getKey();
			String value = entry.getValue();
			// 处理key和value
			result = result + key + ":" + getComString(value, euList) + "\n";
		}
		return result;
	}

	private String getComString(String id, Map<String, EuEntity> euList) {
		String result = "";
		EuEntity eu = euList.get(id);
		if (eu != null) {
			if (eu.getInitwin() > eu.getNewwin()) {
				result = result + " W ";
			}
			if (eu.getInitdraw() > eu.getNewdraw()) {
				result = result + " D ";
			}
			if (eu.getInitlose() > eu.getNewlose()) {
				result = result + " L ";
			}
		}
		return result;
	}

	@Override
	public Site getSite() {
		return site;
	}

	public static void main(String[] args) {
		RaceEntity ce = new RaceEntity();
		ce.setId("1126649");
		new TrendUtil(ce).start();
	}

	public void start() {
		Spider.create(this).addUrl("https://odds.500.com/fenxi/ouzhi-" + this.raceEntity.getId() + ".shtml").run();
	}
}
